package com.appkubes.mall.restful.application.pay.strategy.alipay.controller;

import com.alibaba.fastjson.JSON;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.core.entity.MallBaowen;
import com.appkubes.mall.core.entity.MallPayRecord;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.wx.model.WxModel;
import com.appkubes.mall.restful.application.wx.service.BaowenService;
import com.appkubes.mall.restful.application.wx.service.impl.PayRecordServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/Calipay")
public class AlipayController extends BaseController {


    @Autowired
    BaowenService baowenService;

    @Resource
    PayRecordServiceImpl payRecordService;

    @Autowired
    WxModel wxModel;


    @PostMapping("/paycallback")
    public Object paycallback(@RequestParam(defaultValue = "") String outTradeNo,
                              @RequestParam(defaultValue = "") String tradeNo) {
        //增加报文记录
        //1 获取微信支付异步回调结果
        try {
            //增加报文记录

            MallBaowen baowen = new MallBaowen();
            baowen.setXml(getAllPrams());
            baowen.setOutTradeNo(outTradeNo);
            baowen.setTransactionId(tradeNo);
            baowen.setAddTime(DateUtils.DateToTimestamp(new Date()));
            baowenService.save(baowen);

            String payrecordId = servletRequest.getParameter("body");
            if (!StringUtils.isEmpty(payrecordId)) {
                //业务处理
                MallPayRecord record = payRecordService.getById(payrecordId);
                if (!StringUtils.isEmpty(record.getCallbackTime())) {
                    return "success";
                }
                record.setCallbackTime(DateUtils.DateToTimestamp(new Date()));
                record.setCallbackXml(getAllPrams());
                Boolean ret = payRecordService.updateById(record);
                if (ret) {
                    return "success";
                }
            } else {
                return "fail";
            }
        } catch (Exception e) {
            log.info("结果异常:" + e.getMessage());
            return "fail";
        }
        return "fail";
    }

    public String getAllPrams() {
        Map map = new HashMap();
        Enumeration em = servletRequest.getParameterNames();
        while (em.hasMoreElements()) {
            String paramName = (String) em.nextElement();
            String paramValue = this.servletRequest.getParameter(paramName);
            //形成键值对应的map
            map.put(paramName, paramValue);
        }
        return JSON.toJSONString(map);
    }
}
